﻿namespace OboutInc.Show
{
    using System;
    using System.ComponentModel;
    using System.Runtime.Serialization;
    using System.Web.UI;
    using System.Xml;

    [Serializable, ToolboxItem(false), TypeConverter(typeof(ExpandableObjectConverter))]
    public class Panel : Control, INamingContainer, ISerializable
    {
        public Panel() : this(string.Empty, false, OboutInc.Show.PanelType.InnerHtml)
        {
        }

        protected Panel(SerializationInfo info, StreamingContext context)
        {
        }

        public Panel(string content, OboutInc.Show.PanelType _panelType) : this(content, false, _panelType)
        {
        }

        public Panel(string content, bool _selected, OboutInc.Show.PanelType _panelType)
        {
            switch (_panelType)
            {
                case OboutInc.Show.PanelType.InnerHtml:
                    this.InnerHtml = content;
                    break;

                case OboutInc.Show.PanelType.ImagePath:
                    this.ImagePath = content;
                    break;

                case OboutInc.Show.PanelType.Url:
                    this.Url = content;
                    break;
            }
            this.Selected = _selected;
            this.PanelType = _panelType;
        }

        public Panel(string content, string _imageTitle, bool _selected, OboutInc.Show.PanelType _panelType) : this(content, _selected, _panelType)
        {
            this.ImageTitle = _imageTitle;
        }

        public void Deserialize(XmlElement parentNode)
        {
            this.PanelType = (OboutInc.Show.PanelType) Enum.Parse(typeof(OboutInc.Show.PanelType), parentNode.GetAttribute("PanelType"));
            if (this.PanelType == OboutInc.Show.PanelType.InnerHtml)
            {
                this.InnerHtml = parentNode.GetAttribute("InnerHtml");
            }
            if (this.PanelType == OboutInc.Show.PanelType.ImagePath)
            {
                this.ImagePath = parentNode.GetAttribute("ImagePath");
            }
            if (this.PanelType == OboutInc.Show.PanelType.Url)
            {
                this.Url = parentNode.GetAttribute("Url");
            }
            this.Selected = bool.Parse(parentNode.GetAttribute("Selected"));
            this.ImageTitle = parentNode.GetAttribute("ImageTitle");
        }

        protected override void Render(HtmlTextWriter writer)
        {
            if (this.InnerHtml != string.Empty)
            {
                writer.Write(this.InnerHtml);
            }
            else
            {
                base.Render(writer);
            }
        }

        public void Serialize(ref XmlElement parentNode)
        {
            parentNode.SetAttribute("PanelType", ((int) this.PanelType).ToString());
            parentNode.SetAttribute("InnerHtml", this.InnerHtml);
            parentNode.SetAttribute("ImagePath", this.ImagePath);
            parentNode.SetAttribute("Url", this.Url);
            parentNode.SetAttribute("Selected", this.Selected.ToString());
            parentNode.SetAttribute("ImageTitle", this.ImageTitle);
        }

        void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
        {
        }

        [DefaultValue(""), Category("Settings"), Description("Image Height")]
        public string ImageHeight
        {
            get
            {
                if (this.ViewState["ImageHeight"] == null)
                {
                    this.ViewState["ImageHeight"] = "";
                }
                return (string) this.ViewState["ImageHeight"];
            }
            set
            {
                this.ViewState["ImageHeight"] = value;
            }
        }

        [Category("Settings"), Description("Image Path"), DefaultValue("")]
        public string ImagePath
        {
            get
            {
                if (this.ViewState["ImagePath"] == null)
                {
                    this.ViewState["ImagePath"] = "";
                }
                return (string) this.ViewState["ImagePath"];
            }
            set
            {
                this.ViewState["ImagePath"] = value;
                this.PanelType = OboutInc.Show.PanelType.ImagePath;
            }
        }

        [Description("ImageTitle"), Category("Settings"), DefaultValue("")]
        public string ImageTitle
        {
            get
            {
                if (this.ViewState["ImageTitle"] == null)
                {
                    this.ViewState["ImageTitle"] = "";
                }
                return (string) this.ViewState["ImageTitle"];
            }
            set
            {
                this.ViewState["ImageTitle"] = value;
            }
        }

        [Category("Settings"), DefaultValue(""), Description("Image Width")]
        public string ImageWidth
        {
            get
            {
                if (this.ViewState["ImageWidth"] == null)
                {
                    this.ViewState["ImageWidth"] = "";
                }
                return (string) this.ViewState["ImageWidth"];
            }
            set
            {
                this.ViewState["ImageWidth"] = value;
            }
        }

        [Description("Inner Html"), Category("Settings"), DefaultValue("")]
        public string InnerHtml
        {
            get
            {
                if (this.ViewState["InnerHtml"] == null)
                {
                    this.ViewState["InnerHtml"] = "";
                }
                return (string) this.ViewState["InnerHtml"];
            }
            set
            {
                this.ViewState["InnerHtml"] = value;
                this.PanelType = OboutInc.Show.PanelType.InnerHtml;
            }
        }

        [DefaultValue("PanelType.InnerHtml"), Category("Settings"), Description("Panel Type")]
        public OboutInc.Show.PanelType PanelType
        {
            get
            {
                if (this.ViewState["PanelType"] == null)
                {
                    this.ViewState["PanelType"] = OboutInc.Show.PanelType.InnerHtml;
                }
                return (OboutInc.Show.PanelType) this.ViewState["PanelType"];
            }
            set
            {
                this.ViewState["PanelType"] = value;
            }
        }

        [Description("Selected Panel"), DefaultValue(false), Category("Behavior")]
        public bool Selected
        {
            get
            {
                if (this.ViewState["selected"] == null)
                {
                    this.ViewState["selected"] = false;
                }
                return (bool) this.ViewState["selected"];
            }
            set
            {
                this.ViewState["selected"] = value;
            }
        }

        [Description("Url"), Category("Settings"), DefaultValue("")]
        public string Url
        {
            get
            {
                if (this.ViewState["url"] == null)
                {
                    this.ViewState["url"] = "";
                }
                return (string) this.ViewState["url"];
            }
            set
            {
                this.ViewState["url"] = value;
                this.PanelType = OboutInc.Show.PanelType.Url;
            }
        }

        [Description("Client script to trigger when the URL is loaded."), DefaultValue(""), Category("Settings")]
        public string URLPanel_OnLoadScript
        {
            get
            {
                if (this.ViewState["URLPanel_OnLoadScript"] == null)
                {
                    this.ViewState["URLPanel_OnLoadScript"] = string.Empty;
                }
                return (string) this.ViewState["URLPanel_OnLoadScript"];
            }
            set
            {
                this.ViewState["URLPanel_OnLoadScript"] = value;
            }
        }
    }
}

